// 微信支付
/**
*  App.Util.wechatPay(Object.assign(数据, {
              success(res) {
                //支付成功
              },
              fail(res) {
                //支付失败
              }
            }))
* 
*/
const ajax = require("./request.js");
const Api = require("./api.js").Api;
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
var util = {
  //获取当天是否签到过
  dateNowadays(ck) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_today = date.getDate();
    let that = this
    let signUp = [];
    let state = false;//是否签到过
    ajax(Api.checkIn, 'GET', {}).then(res => {
      signUp = [...res.data.rows]
      for (var i = 0; i < signUp.length; i++) {
        var current = new Date(signUp[i].check_in_date.replace(/-/g, "/"));
        // console.log('current', current)
        var year = current.getFullYear(); //签到的年
        var month = current.getMonth() + 1;//签到的月
        var day = current.getDate();//签到的日
        day = parseInt(day);

        //判断当天是否签到过
        if (year == cur_year && month == cur_month && day == cur_today) {

          state = true
          ck && ck(state)
        }

      }


    }).catch(err => {
      console.log('报错了', err)
    })

  },
  wechatPay(options) {
    console.log('wechatPay', options)
    options = options || {}
    wx.requestPayment({
      'timeStamp': options.timeStamp,
      'nonceStr': options.nonceStr,
      'signType': options.signType || "MD5",
      'package': options.package,
      'paySign': options.paySign,
      success(res) {
        console.log("wechatPay123123", res)
        options.success && options.success(res)
      },
      fail(res) {
        console.log(res, res.errMsg, res.errMsg === "requestPayment:fail cancel")
        options.fail && options.fail(res)
        if (res.errMsg === "requestPayment:fail cancel") {
          wx.showToast({
            title: res.msg || '取消支付！',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: res.msg || '支付失败！',
            icon: 'none',
            success: function () {

            }
          })

        }
      }
    })
  },

  /**
   * 时间戳转化为年 月 日 时 分 秒
   * ts: 传入时间戳
   * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
  */
  formatDate(timestamp, format) {
    const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    let returnArr = [];

    let date = new Date(timestamp);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()
    returnArr.push(year, month, day, hour, minute, second);

    returnArr = returnArr.map(formatNumber);

    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  }
}


module.exports = {
  util
}
